//
//  ViewController.m
//  DogWalk
//
//  Created by Mac on 15/11/15.
//  Copyright © 2015年 riverlj. All rights reserved.
//

#import "ViewController.h"
#import "Dog.h"
#import "Walk.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableiew;
@property (nonatomic, strong)Dog *currentDog;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableiew registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    NSFetchRequest *dogFetch = [[NSFetchRequest alloc]init];
    NSEntityDescription *dogEntity = [NSEntityDescription entityForName:@"Dog" inManagedObjectContext:self.managedContext];
    [dogFetch setEntity:dogEntity];

    NSString *dogName = @"Fibi";
    dogFetch.predicate = [NSPredicate predicateWithFormat:@"name == %@",dogName];
    NSError *error = nil;
    NSArray *results = [self.managedContext executeFetchRequest:dogFetch error:&error];
    if (error) {
        NSLog(@"error = %@", error);
    }
    
    if (results.count>0) {
        self.currentDog = [results objectAtIndex:0];
        NSLog(@"--------%ld",self.currentDog.walks.count);
    }else {
        self.currentDog = [[Dog alloc]initWithEntity:dogEntity insertIntoManagedObjectContext:self.managedContext];
        self.currentDog.name = dogName;
        [self.managedContext save:&error];
        if (error) {
            NSLog(@"%@",error);
        }
    }
}

- (IBAction)add:(UIBarButtonItem *)sender {
    NSEntityDescription *walkEntity = [NSEntityDescription entityForName:@"Walk" inManagedObjectContext:self.managedContext];
    Walk *walk = [[Walk alloc]initWithEntity:walkEntity insertIntoManagedObjectContext:self.managedContext];
    walk.date = [[NSDate alloc]init];
    NSMutableOrderedSet *walks = [self.currentDog.walks mutableCopy];
    [walks addObject:walk];
    self.currentDog.walks = [walks copy];
    
    
    NSError *error = nil;
    [self.managedContext save:&error];
    if (error) {
        NSLog(@"error = %@", error);
    }
    [self.tableiew reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%ld", self.currentDog.walks.count);
    return self.currentDog.walks.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    Walk *walk = [self.currentDog.walks array][indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", walk.date];
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSMutableSet *walks = (NSMutableSet *)self.currentDog.walks;
        Walk *removeWalk = [walks allObjects][indexPath.row];
        [walks removeObject:removeWalk];
        self.currentDog.walks = [walks copy];
    
        [self.managedContext deleteObject:removeWalk];
        NSError *error = nil;
        [self.managedContext save:&error];
        if (error) {
            NSLog(@"error=%@",error);
        }
        
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"List of Walks";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
